package hyit.app.servlet.cilent;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

/**
 * Servlet implementation class CheckVersionJSON
 */
@WebServlet("/checkVersionJSON")
public class CheckVersionJSON extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CheckVersionJSON() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	@SuppressWarnings("deprecation")
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		String realPath = request.getRealPath(File.separator);
		File file = new File(realPath+"/version.ini");
		BufferedReader br = new BufferedReader(new FileReader(file));
		String str = null;
		JSONObject jsonObj = new JSONObject();
		PrintWriter pw = response.getWriter();
		while((str=br.readLine())!=null){
			String[] arr = str.split("=");
			jsonObj.put(arr[0], arr[1]);
		}
		pw.print(jsonObj);
		br.close();
		pw.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
